package com.lujianfei.rxjavademo;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import androidx.appcompat.app.AppCompatActivity;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    static final String TAG = "MainActivity";

    List<Student> students = new ArrayList<>();
    List<School> schools = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        findViewById(R.id.filter).setOnClickListener(this);
        findViewById(R.id.Map).setOnClickListener(this);
        findViewById(R.id.flatMap).setOnClickListener(this);
        findViewById(R.id.sort).setOnClickListener(this);
    }

    private void initData() {
        students.add(new Student("小明", "男", 80));
        students.add(new Student("小红", "女", 90));
        students.add(new Student("小芳", "女", 70));
        students.add(new Student("小李", "男", 60));
        students.add(new Student("张三", "男", 50));
        students.add(new Student("李四", "男", 58));

        schools.add(new School("清华大学", students));
        schools.add(new School("北京大学", students));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.filter: {
                filter();
            }
            break;
            case R.id.Map: {
                Map();
            }
            break;
            case R.id.flatMap: {
                flatMap();
            }
            break;
            case R.id.sort: {
                sort();
            }
            break;
        }
    }

    private void filter() {
        // 找到分数 >= 60 的男同学
        Observable
                .fromIterable(students)
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) throws Exception {
                        return student.score >= 60 && "男".equals(student.sex);
                    }
                }).toList().subscribe(new Consumer<List<Student>>() {
            @Override
            public void accept(List<Student> students) throws Exception {
                Log.d(TAG, "" + students);
            }
        });
    }

    private void Map() {
        // 输出所有的学生姓名
        Observable
                .fromIterable(students)
                .map(new Function<Student, String>() {
                    @Override
                    public String apply(Student student) throws Exception {
                        return student.name;
                    }
                })
                .toList()
                .subscribe(new Consumer<List<String>>() {
            @Override
            public void accept(List<String> strings) throws Exception {
                Log.d(TAG, "" + strings);
            }
        });
    }

    private void flatMap() {
        // 输出所有学校的学生姓名，用flatMap实现数据结构的降维，父级降到子级
        Observable
                .fromIterable(schools)
                .flatMap(new Function<School, ObservableSource<Student>>() {
                    @Override
                    public ObservableSource<Student> apply(School school) throws Exception {
                        return Observable.fromIterable(school.studentList);
                    }
                })
                .map(new Function<Student, String>() {
                    @Override
                    public String apply(Student student) throws Exception {
                        return student.name;
                    }
                })
                .toList()
                .subscribe(new Consumer<List<String>>() {
                    @Override
                    public void accept(List<String> strings) throws Exception {
                        Log.d(TAG, "" + strings);
                    }
                });
    }

    private void sort() {
        // 学生按分数从高到低排列
        Observable
                .fromIterable(students)
                .toSortedList(new Comparator<Student>() {
                    @Override
                    public int compare(Student o1, Student o2) {
                        return o2.score - o1.score;
                    }
                })
                .subscribe(new Consumer<List<Student>>() {
                    @Override
                    public void accept(List<Student> students) throws Exception {
                        Log.d(TAG, "" + students);
                    }
                });
    }

}
